﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MxWeiXinPF.Common;

namespace MxWeiXinPF.Web.admin.users
{
    public partial class SendMessageedit : MxWeiXinPF.Web.UI.ManagePage
    {
        protected string action = MXEnums.ActionEnum.Add.ToString();
        protected MxWeiXinPF.Model.manager admin_info;
        private int id = 0;
        protected int wid = 0;

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (this.action == MXEnums.ActionEnum.Edit.ToString())
            {
                base.ChkAdminLevel("SendMessagelist", MXEnums.ActionEnum.Edit.ToString());
                if (!this.DoEdit(this.id))
                {
                    base.JscriptMsg("保存过程中发生错误！", "", "Error");
                }
                else
                {
                    base.JscriptMsg("修改成功！", "SendMessagelist.aspx", "Success");
                }
            }
            else
            {
                base.ChkAdminLevel("SendMessagelist", MXEnums.ActionEnum.Add.ToString());
                if (!this.DoAdd())
                {
                    base.JscriptMsg("保存过程中发生错误！", "", "Error");
                }
                else
                {
                    base.JscriptMsg("添加成功！", "SendMessagelist.aspx", "Success");
                }
            }
        }

        private bool DoAdd()
        {
            bool flag = false;
            MxWeiXinPF.Model.sendmessage model = new MxWeiXinPF.Model.sendmessage();
            MxWeiXinPF.BLL.sendmessage sendmessage2 = new MxWeiXinPF.BLL.sendmessage();
            model.wid = this.wid;
            if (this.admin_info != null)
            {
                model.shopid = this.admin_info.id;
            }
            else
            {
                this.admin_info = base.GetAdminInfo();
                model.shopid = this.admin_info.id;
            }
            model.truename = this.txtUserName.Text.Trim();
            model.mobile = this.txtMobile.Text.Trim();
            model.MessageContent = this.txtMessageContent.Text.Trim();
            model.productname = this.txtproductname.Text.Trim();
            model.PurchaseQty = int.Parse(this.txtPurchaseQty.Text.Trim());
            model.UsedQty = int.Parse(this.txtUsedQty.Text.Trim());
            model.PurchaseDate = DateTime.Parse(this.txtPurchaseDate.Text);
            model.tqts = int.Parse(this.ddltqts.SelectedValue.Trim());
            if (sendmessage2.AddInfo(model) > 0)
            {
                base.AddAdminLog(MXEnums.ActionEnum.Add.ToString(), "手工添加用药提醒:" + model.truename);
                flag = true;
            }
            return flag;
        }

        private bool DoEdit(int _id)
        {
            bool flag = false;
            MxWeiXinPF.BLL.sendmessage sendmessage = new MxWeiXinPF.BLL.sendmessage();
            MxWeiXinPF.Model.sendmessage model = sendmessage.GetModel(_id);
            model.wid = this.wid;
            if (this.admin_info != null)
            {
                model.shopid = this.admin_info.id;
            }
            else
            {
                this.admin_info = base.GetAdminInfo();
                model.shopid = this.admin_info.id;
            }
            model.truename = this.txtUserName.Text.Trim();
            model.mobile = this.txtMobile.Text.Trim();
            model.MessageContent = this.txtMessageContent.Text.Trim();
            model.productname = this.txtproductname.Text.Trim();
            model.PurchaseQty = int.Parse(this.txtPurchaseQty.Text.Trim());
            model.UsedQty = int.Parse(this.txtUsedQty.Text.Trim());
            model.PurchaseDate = DateTime.Parse(this.txtPurchaseDate.Text);
            model.tqts = int.Parse(this.ddltqts.SelectedValue.Trim());
            model.id = _id;
            if (sendmessage.Update(model))
            {
                base.AddAdminLog(MXEnums.ActionEnum.Edit.ToString(), "修改用户信息:" + model.truename);
                flag = true;
            }
            return flag;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                string queryString = MXRequest.GetQueryString("action");
                MxWeiXinPF.Model.manager weiXinCode = base.GetAdminInfo();
                this.wid = weiXinCode.id;
                if (!string.IsNullOrEmpty(queryString) && (queryString == MXEnums.ActionEnum.Edit.ToString()))
                {
                    this.action = MXEnums.ActionEnum.Edit.ToString();
                    this.id = MXRequest.GetQueryInt("id");
                    if (this.id == 0)
                    {
                        base.JscriptMsg("传输参数不正确！", "back", "Error");
                        return;
                    }
                    if (!new MxWeiXinPF.BLL.sendmessage().Exists(this.id))
                    {
                        base.JscriptMsg("信息不存在或已被删除！", "back", "Error");
                        return;
                    }
                }
                if (!this.Page.IsPostBack)
                {
                    base.ChkAdminLevel("SendMessagelist", MXEnums.ActionEnum.View.ToString());
                    this.admin_info = base.GetAdminInfo();
                    if (this.action == MXEnums.ActionEnum.Edit.ToString())
                    {
                        this.ShowInfo(this.id);
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }

        private void ShowInfo(int _id)
        {
            MxWeiXinPF.Model.sendmessage model = new MxWeiXinPF.BLL.sendmessage().GetModel(_id);
            this.txtUserName.Text = model.truename;
            this.txtMobile.Text = model.mobile;
            this.txtMessageContent.Text = model.MessageContent;
            this.txtproductname.Text = model.productname;
            this.txtPurchaseQty.Text = model.PurchaseQty.ToString();
            this.txtUsedQty.Text = model.UsedQty.ToString();
            this.txtPurchaseDate.Text = model.PurchaseDate.ToString("yyyy-MM-dd");
            if (!model.tqts.ToString().Trim().Equals("") && (this.ddltqts.Items.FindByValue(model.tqts.ToString().Trim()) != null))
            {
                this.ddltqts.SelectedIndex = -1;
                this.ddltqts.Items.FindByValue(model.tqts.ToString().Trim()).Selected = true;
            }
        }
    }
}